package com.kingdee.shr.recuritment.web.handler;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.ui.ModelMap;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.metadata.entity.DataType;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.framework.DataBaseInfo;
import com.kingdee.eas.hr.rec.RecAppCmpItemCollection;
import com.kingdee.eas.hr.rec.RecAppCmpItemFactory;
import com.kingdee.eas.hr.rec.RecAppCmpItemInfo;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.util.SHRPermissionWebUtils;
import com.kingdee.shr.base.syssetting.web.dynamic.model.FieldInfo;
import com.kingdee.shr.base.syssetting.web.dynamic.model.UIViewInfo;
import com.kingdee.shr.base.syssetting.web.dynamic.util.UIViewUtil;
import com.kingdee.shr.base.syssetting.web.handler.CoreHandler;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
import com.kingdee.shr.compensation.request.ParseRequestHelper;
import com.kingdee.shr.compensation.util.CmpDomUtil;

public class RecAppCmpItemHandler extends CoreHandler {
	private String display = "com.kingdee.shr.recuritment.app.RecAppCmpItem.display";
	
	@Override
	protected void setInitData(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,
			Map<String, Object> initData) throws SHRWebException {
		super.setInitData(request, response, modelMap, initData);
	}
	
	
	public String getFieldListConfigAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
			throws SHRWebException {
		List<FieldInfo> fields = this.getFixAdjustListFields();
		JSONUtils.SUCCESS(fields);
		return null;
	}

	private List<FieldInfo> getFixAdjustListFields() throws SHRWebException {
		UIViewInfo uiViewInfo = UIViewUtil.getUIViewInfoByUIPK(this.display);
		String content = uiViewInfo.getContent();
//		List<FieldInfo> fields = CmpDomUtil.parseFieldByUIViewContent(content);
		List<FieldInfo> fields = new ArrayList();
		Document doc = null;

		try {
			doc = DocumentHelper.parseText(content);
		} catch (DocumentException var6) {
			var6.printStackTrace();
			throw new SHRWebException(var6.getMessage());
		}

		Element root = doc.getRootElement();
		Element body = root.element("body");
		List<Element> elements = body.elements("field");
		if (elements != null && elements.size() != 0) {
			int i = 0;
			for (int size = elements.size(); i < size; ++i) {
				FieldInfo field = new FieldInfo();
				Element element = (Element) elements.get(i);
				field.setName(element.attributeValue("name"));
				field.setAlias(element.attributeValue("label"));
				field.setDataType(element.attributeValue("dataType"));
				fields.add(field);
			}
			return fields;
		} else {
			return fields;
		}
	}
	
	public String getLatestDataAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
			throws SHRWebException {
		String recApprovalId = request.getParameter("recApprovalId");
		Map<String, Object> ajaxData = new HashMap();
		List<Object> datas = this.getLatestData(request, recApprovalId);
		ajaxData.put("listData", datas);
		JSONUtils.writeJson(response, ajaxData);
		return null;
	}
	
	public List<Object> getLatestData(HttpServletRequest request, String recApprovalId)
			throws SHRWebException {
		String permItemId = SHRPermissionWebUtils.getCurrentRequestPermItemId(request);
		List<Object> datas = new ArrayList();
		List<FieldInfo> fields = this.getFixAdjustListFields();
		RecAppCmpItemCollection coll;
		try {
			coll = RecAppCmpItemFactory.getRemoteInstance().getRecAppCmpItemCollection("where recApproval.id = '"+recApprovalId+"'");
		} catch (BOSException e) {
			e.printStackTrace();
			throw new SHRWebException(e.getMessage());
		}
		
		
		for (int i = 0; i < coll.size(); ++i) {
			RecAppCmpItemInfo info = coll.get(i);
			Map<String, Object> map = this.getFixAdjustSalaryInfoMap(info, fields);
			datas.add(map);
		}

		return datas;
	}

	private Map<String, Object> getFixAdjustSalaryInfoMap(RecAppCmpItemInfo info, List<FieldInfo> fields) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Map<String, Object> map = new HashMap();
		Map<String, String> f7map = null;
		Iterator i$ = fields.iterator();
		while (i$.hasNext()) {
			FieldInfo field = (FieldInfo) i$.next();
			CoreBaseInfo coreInfo = info;
			String fullName = field.getName();
			String dataType = null == field.getDataType() ? "" : field.getDataType().getName();
			String[] split = fullName.split("\\.");

			for (int j = 0; j < split.length; ++j) {
				String fieldName = split[j];
				if (j != split.length - 1) {
					coreInfo = (CoreBaseInfo) ((CoreBaseInfo) coreInfo).get(fieldName);
					if (coreInfo == null) {
						map.put(fullName, "");
						break;
					}
				} else if (DataType.OBJECTVALUE.getName().equalsIgnoreCase(dataType)) {
					DataBaseInfo dataInfo = (DataBaseInfo) ((CoreBaseInfo) coreInfo).get(fieldName);
					f7map = new HashMap();
					f7map.put("id", dataInfo == null ? "" : dataInfo.getId().toString());
					f7map.put("name", dataInfo == null ? "" : dataInfo.getName());
					map.put(fullName, f7map);
				} else if (DataType.DATE.getName().equalsIgnoreCase(dataType)) {
					map.put(fullName, sdf.format(((CoreBaseInfo) coreInfo).get(fieldName)));
				} else {
					map.put(fullName, ((CoreBaseInfo) coreInfo).get(fieldName) == null
							? ""
							: ((CoreBaseInfo) coreInfo).get(fieldName));
				}
			}
		}
		map.put("adjEmpORelation",
				null == info.getRecApproval() ? "" : info.getRecApproval().getId().toString() + "");
		map.put("id", info.getId() + "");
		map.put("id_add", info.getId() + "");
		return map;
	}
}
